package com.gitee.easyopen.server.api;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.gitee.easyopen.annotation.Api;
import com.gitee.easyopen.annotation.ApiService;
import com.gitee.easyopen.server.message.GoodsErrors;
import com.gitee.easyopen.server.model.Goods;
import com.gitee.easyopen.server.model.GoodsParam;

/**
 * 业务类
 * 
 * @author tanghc
 *
 */
@ApiService
public class GoodsApi {

    @Autowired
    UserService userService;

    @Api(name = "goods.get")
    public Goods getGoods(GoodsParam param) {
        if(StringUtils.isEmpty(param.getGoods_name())) {
            throw GoodsErrors.NO_GOODS_NAME.getException();
        }
        System.out.println("user:" + userService.getUser());
        Goods goods = new Goods();
        goods.setId(1L);
        goods.setGoods_name("iPhoneX");
        goods.setPrice(new BigDecimal(8000));
        return goods;
    }

    @Api(name = "goods.list", version = "2.0")
    public List<Goods> listGoods(GoodsParam param) {
        System.out.println("user:" + userService.getUser());
        Goods goods = new Goods();
        goods.setId(1L);
        goods.setGoods_name("iPhoneX");
        goods.setPrice(new BigDecimal(8000));

        Goods goods2 = new Goods();
        goods2.setId(2L);
        goods2.setGoods_name("三星");
        goods2.setPrice(new BigDecimal(7000));
        return Arrays.asList(goods, goods2);
    }

    // 该接口不需要验证
    @Api(name = "goods.add",ignoreValidate = true)
    public void addGoods(GoodsParam param) {
        if (StringUtils.isEmpty(param.getGoods_name())) {
            throw GoodsErrors.NO_GOODS_NAME.getException();
        }
        if(param.getGoods_name().length() < 3) {
            throw GoodsErrors.SHORT_GOODS_NAME.getException(3);
        }
        Goods goods = new Goods();
        goods.setId(2L);
        goods.setGoods_name(param.getGoods_name());
        goods.setPrice(new BigDecimal(8000));
    }

}
